package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxuwz.medical.dao.PersonDao;
import com.gxuwz.medical.domain.pay.Pay;
import com.gxuwz.medical.domain.pay.Standard;
import com.gxuwz.medical.domain.person.Person;
import com.gxuwz.medical.domain.user.User;
import com.gxuwz.medical.vo.PageBean;

public class PayServlet extends HttpServlet {


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String method = request.getParameter("method");
		try {
			if(method.equals("list")){
				list(request,response);
			}else if(method.equals("serch")){
				serch(request,response);
			}else if(method.equals("gotopay")){
				gotopay(request,response);
			}else if(method.equals("add")){
				add(request,response);
			}else if(method.equals("setStandard")){
				setStandard(request,response);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void setStandard(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException, SQLException, ParseException{
		String stimestr = request.getParameter("stime");
		String etimestr = request.getParameter("etime");
		String moneystr = request.getParameter("money");
		double money = Double.parseDouble(moneystr);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date stime = df.parse(stimestr);
		Date etime = df.parse(etimestr);
		Standard s = new Standard(money,stime,etime);
		if(request.getParameter("reset")!=null&&request.getParameter("reset").equals("yes"))
			s.edit();
		else
			s.add();
		list(request, response);
	}

	private void gotopay(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException, SQLException{
		// TODO Auto-generated method stub
		String path = "/page/pay/pay_add.jsp";
		String familycode = request.getParameter("familycode");
		Pay p = new Pay();
		Standard s = new Standard();
		if(!s.hasStandard){
			request.setAttribute("msg", "本年度未设置缴费标准");
			list(request,response);
			return;
		}else if(!s.isOnTime){
			request.setAttribute("msg", "不在缴费时间内");
			list(request,response);
			return;
		}else if(p.isPayFinish(familycode)){
			request.setAttribute("msg", "无未缴费费成员");
			list(request,response);
			return;
		}else{
			request.setAttribute("noPayPerson",p.queryNoPayPerson(familycode));
			request.getRequestDispatcher(path).forward(request, response);
			return;
		}
	}
	
	private void add(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException, SQLException{
		double money = Double.parseDouble(request.getParameter("money"));
		String[] ybcardnoarr = request.getParameterValues("ybcardno");
		User user =new User();
		user=(User)request.getSession().getAttribute("user");
		String username = user.getUserid();
		for(int i=0;i<ybcardnoarr.length;i++){
			String ybcardno = ybcardnoarr[i];
			Pay p = new Pay(ybcardno,money);
			p.setUsername(username);
			p.add();
		}
		request.setAttribute("msg", "缴费完成");
		list(request, response);
	}
	private void serch(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException, SQLException {
		// TODO Auto-generated method stub
		String path = "/page/pay/pay_list.jsp";
		PersonDao dao = new PersonDao();
		String page = request.getParameter("page");
		String fullname = request.getParameter("fullname");
		fullname = new String(fullname.getBytes("ISO-8859-1"),"UTF-8");
		System.out.println(fullname);
		if (page == null)
			page = "1";
		int currentpage = Integer.parseInt(page);
		PageBean pageBean = dao.queryHolderByName(fullname, currentpage, 5);
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher(path).forward(request, response);
	}


	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException{
		// TODO Auto-generated method stub
		String path = "/page/pay/pay_list.jsp";
		PersonDao dao = new PersonDao();
		String page = request.getParameter("page");
		if (page == null)
			page = "1";
		int currentpage = Integer.parseInt(page);
		PageBean pageBean = dao.queryAllHolder(currentpage, 5);
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher(path).forward(request, response);
	}

}
